<?php
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$commentTable = 'hellozjx_mapwxapp_comment';
$userTable = 'hellozjx_mapwxapp_user';
$videoTable = 'hellozjx_mapwxapp_video';
$signRelation = 'hellozjx_mapwxapp_comment_signrelation';
$signTable = 'hellozjx_mapwxapp_comment_sign';
$check_status = [0 => '未审核' ,1  => '已通过' , 2 => '未通过'];

if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $list = pdo_fetchall("SELECT c.id,c.title,c.check_status,c.satisfaction,c.create_time,u.nick_name,v.name FROM " . tablename($commentTable) . " c left join ".tablename($userTable)." u on u.id=c.uid left join ".tablename($videoTable)." v on v.id=c.vid where c.status = 1 ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($commentTable) . ' where status = 1');
    $pager = pagination($total, $pindex, $psize);
} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    if (checksubmit('submit')) {
        if(empty($_GPC['check_status']))
            message('操作失败！', $this->createWebUrl('comment', array('op' => 'display')), 'error');
        $data = array(
            'check_status' => $_GPC['check_status'],
            'check_time' =>date('Y-m-d H:i:s',time())
        );
        if (!empty($id)) {  //添加、修改教师信息表
            pdo_update($commentTable, $data, array('id' => $id));
        } else {
            pdo_insert($commentTable, $data);
            $id = pdo_insertid();
            if(!$id){
                message('操作失败！', $this->createWebUrl('comment', array('op' => 'display')), 'error');
            }
        }
        message('更新信息成功！', $this->createWebUrl('comment', array('op' => 'display')), 'success');
    }

    $comment = pdo_fetch("SELECT c.id,c.title,c.content,c.check_status,c.satisfaction,c.create_time,u.nick_name,v.name FROM " . tablename($commentTable) . " c left join ".tablename($userTable)." u on u.id=c.uid left join ".tablename($videoTable)." v on v.id=c.vid where c.id={$id}");
    $sign = pdo_fetchall("SELECT s.name from " .tablename($commentTable) ." c left join ".tablename($signRelation)." sr on sr.comment_id=c.id left join ".tablename($signTable)." s on s.id=sr.sign_id where c.id={$id}");
}elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $comment = pdo_fetch("SELECT id FROM " . tablename($commentTable) . " WHERE id = '{$id}'");
    if (empty($comment)) {
        message('数据有误', $this->createWebUrl('comment', array('op' => 'display')), 'error');
    }
    pdo_update($commentTable, ['status' => 0], array('id' => $id));
    message('删除成功', referer(), 'success');
}
include $this->template('comment');